package com.example.kafka.controller;



import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.kafka.support.KafkaHeaders;
import org.springframework.messaging.MessageChannel;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/home")
public class HomeController {



    private final MessageChannel channel;

    public HomeController(@Qualifier("toKafka") MessageChannel channel) {
        this.channel = channel;
    }

    @GetMapping(value = "/send")
    public void send(){
        System.out.println("准备发送");
        for (int i = 0 ;i<10 ;i++){
            channel.send(MessageBuilder.withPayload("cwy"+i).setHeader(KafkaHeaders.TOPIC,"sad").setHeader(KafkaHeaders.MESSAGE_KEY,1).build());
        }
    }
}
